<template>
  <basic-container>
    <div class="accountAddTable">
      <avue-crud :option="option"
                 :table-loading="loading"
                 ref="crud"
                 :data="data"
                 :page.sync="page"
                 v-model="form"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 @selection-change="selectionChange"
                 @current-change="currentChange"
                 @size-change="sizeChange"
                 @refresh-change="refreshChange"
                 @on-load="onLoad">
        <template slot="menuLeft">
          <el-button id="id-business-components-offershareaccuntadd-handleClearSelection"
                     type="success"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     :disabled="selectionFinalList.length == 0"
                     @click="handleClearSelection">清空选择
          </el-button>
          已选账户：{{selectionPlatformIds}}
        </template>
      </avue-crud>
    </div>
  </basic-container>
</template>

<script>
import {getOfferAccountPage as getAccountList} from "@/api/business/account";
import {addAccountOffers} from "@/api/business/share"


export default {
  name:'offerShareAccountAdd',
  props: {
    targetUserId: {
      type: String
    },
    excludePlatformIds: {
      type: Array,
    },
  },
  data() {
    return {
      search: {},
      loading: true,
      form: {},
      query: {},
      data: [],

      option: {
        calcHeight: 80,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        dialogClickModal: false,
        reserveSelection: true,
        rowKey: 'id',
        column: [
          {
            label: "账户名",
            prop: "name",
            rules: [{
              required: true,
              message: "请输入名称",
              trigger: "blur"
            }],
            searchSpan:8,
            search: true,
          },
          {
            label: "账户ID",
            prop: "platformId",
            rules: [{
              required: true,
              message: "请输入账户ID",
              trigger: "blur"
            }],
            searchSpan:8,
            search:true
          },
        ],
        addBtn: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
    }
  },
  computed: {
    selectionPlatformIds() {
      let list = [];
      this.selectionList.forEach(row=>{
        if(this.excludePlatformIds == null || !this.excludePlatformIds.includes(row.platformId)) {
          list.push(row.platformId);
        }
      })
      if(list.length == 0) {
        list.push('无');
      }
      return list.join(', ');
    },
    selectionFinalList() {
      let list = [];
      this.selectionList.forEach(row=>{
        if(this.excludePlatformIds == null || !this.excludePlatformIds.includes(row.platformId)) {
          list.push(row);
        }
      })
      return list;
    },
  },
  methods: {
    onLoad(page, params = {}) {
      this.loading = true;
      getAccountList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, {targetUserId: this.targetUserId}),
        this.$parent.$parent.userId,
        this.$parent.$parent.offerId
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      this.query = {};
      this.treeDeptId = '';
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    handleClearSelection() {
      this.$refs.crud.selectClear();
    },
    //提交 account新增
    submitAccountAdd(){
      if (this.selectionFinalList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      let list = []
      this.selectionFinalList.map(item =>{
        let listItem = {
          accountId: item.id,
          accountName: item.name,
          offerId:this.$parent.$parent.offerId,
          permission: 1,
          platformId: item.platformId,
          userId: this.$parent.$parent.userId,
        }
        list.push(listItem)
      })
      addAccountOffers(list).then( res =>{
        if(res.data.code === 200){
          this.$message({type:'success',message:'新增账号成功!'})
          this.$parent.$parent.accountAddDialog = false
          this.$parent.$parent.onloadAccount(this.$parent.$parent.pageAccount,{userId:this.$parent.$parent.userId})
        }
      } )
    }
  }
}

</script>








